package com.example.rdcproject.ving.notes.view;

import com.example.rdcproject.ving.notes.dao.AdminDao;
import com.example.rdcproject.ving.notes.dao.UserDao;
import com.example.rdcproject.ving.notes.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

import static com.sun.javafx.application.PlatformImpl.exit;


public class SwingLogin {
    public static void main(String[] args) throws Exception {
        //创建 JFrame
        JFrame frame =new JFrame("登录");

        //设置高和宽
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        /**
         * 顶层容器
         */
        JPanel panel1 = new JPanel();
         panel1.setLayout(null);
        //添加面板
        frame.add(panel1);
        /**
         * 调用自定义方法添加组件到面板
         */
        addComponents1(panel1);

        //设置页面可见
        frame.setVisible(true);
    }


    private static  void addComponents1(JPanel panel){

        //创建JLabel
        JLabel userlabel = new JLabel("用户名：");
        userlabel.setBounds(10,50,50,30);
        panel.add(userlabel);

        /**
         * 创建文本域用于用户输入
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,50,200,30);
        panel.add(userText);

        //标签
        JLabel passwordlabel = new JLabel("密码：");
        passwordlabel.setBounds(10,100,50,30);
        panel.add(passwordlabel);

        /**
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置，由 width 和 height 指定新的大小。
         */
        //输入密码的文本域
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,100,200,30);
       panel.add(passwordText);

        //创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(100,200,70,30);
        panel.add(loginButton);

        //创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(300,200,70,30);
        panel.add(registerButton);

        //创建管理员登录按钮
        JButton adminLoginButton = new JButton("管理员登录");
        adminLoginButton.setBounds(100,300,200,30);
        panel.add(adminLoginButton);

        //激活登录按钮
        loginButton.addActionListener(new ActionListener()  {

            @Override
            public void actionPerformed(ActionEvent e) {

        String account = userText.getText();
        String password = new String(passwordText.getPassword());
                try {
                    //反射获取类对象
                    Class u=UserDao.class;
                    //提取单个方法对象
                    Method  loginMethod = u.getDeclaredMethod("login",String.class,String.class);

                    loginMethod.setAccessible(true);

                    UserDao userDao = new UserDao();
                    Boolean result = (Boolean) loginMethod.invoke(userDao,account,password);
                    //通过登录的返回值判断是否进入页面
                    if (result ==false){

                        exit();
                    }
                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
        });


        //激活管理员登录按钮
        adminLoginButton.addActionListener(new ActionListener()  {
            @Override
            public void actionPerformed(ActionEvent e) {
                addComponents3();

            }
        });


        //激活注册按钮，跳转页面
        registerButton.addActionListener(new ActionListener()  {
            @Override
            public void actionPerformed(ActionEvent e) {
                addComponents2();

            }
        });

    }

    private static  void addComponents2(){
        JFrame frame2 = new JFrame("注册");
        frame2.setLayout(new BorderLayout());

        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());

        frame2.add(panel2);

        frame2.setSize(500,500);
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        //创建JLabel
        JLabel userlabel = new JLabel("用户名：");
        userlabel.setBounds(10,50,50,30);
        panel2.add(userlabel);

        //标签
        JLabel passwordlabel = new JLabel("密码：");
        passwordlabel.setBounds(10,100,50,30);
        panel2.add(passwordlabel);
        //标签2
        JLabel passwordlabel2 = new JLabel("再次输入密码：");
        passwordlabel.setBounds(10,150,50,30);
        panel2.add(passwordlabel2);

        /**
         * 创建文本域用于用户输入
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,50,200,30);
        panel2.add(userText);

        /**
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置，由 width 和 height 指定新的大小。
         */
        //输入密码的文本域
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,100,200,30);
        panel2.add(passwordText);

        //再次输入密码的文本域
        JTextField passwordText2 = new JTextField(20);
        passwordText.setBounds(100,150,200,30);
        panel2.add(passwordText2);

        //创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(300,200,70,30);
        panel2.add(registerButton);


        //激活注册按钮
        registerButton.addActionListener(new ActionListener()  {

            @Override
            public void actionPerformed(ActionEvent e) {
                String account  = userText.getText();
                String password = passwordText.getText();
                String againPassword = passwordText2.getText();
                try {
                    addComponents2();
                    //反射获取类对象
                    Class u=UserDao.class;
                    //提取单个方法对象
                    Method  registerMethod = u.getDeclaredMethod("register",String.class,String.class,String.class);

                    registerMethod.setAccessible(true);

                    UserDao userDao = new UserDao();
                    Boolean result = (Boolean) registerMethod.invoke(userDao,account,password,againPassword);
                    if (result ==true) {
                        JOptionPane.showMessageDialog(null, "你的注册已完成");
                        frame2.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null, "注册失败！");
                        frame2.dispose();
                    }

                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
        });
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       frame2.pack();
        frame2.setVisible(true);
    }

    private static  void addComponents3(){
        JFrame frame3 = new JFrame("管理员登录");
        frame3.setLayout(new BorderLayout());

        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout());

        frame3.add(panel3);

        frame3.setSize(500,500);
        frame3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        //创建JLabel
        JLabel userlabel = new JLabel("管理用户名：");
        userlabel.setBounds(10,50,50,30);
        panel3.add(userlabel);

        //标签
        JLabel passwordlabel = new JLabel("密码：");
        passwordlabel.setBounds(10,100,50,30);
        panel3.add(passwordlabel);

        /**
         * 创建文本域用于用户输入
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,50,200,30);
        panel3.add(userText);

        //输入密码的文本域
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,100,200,30);
        panel3.add(passwordText);

        //创建注册按钮
        JButton entryButton = new JButton("登录");
        entryButton.setBounds(300,200,70,30);
        panel3.add(entryButton);


        //激活登录按钮
        entryButton.addActionListener(new ActionListener()  {

            @Override
            public void actionPerformed(ActionEvent e) {
                String account  = userText.getText();
                String password = passwordText.getText();
                try {
                    addComponents3();
                    //反射获取类对象
                    Class a=AdminDao.class;
                    //提取单个方法对象
                    Method  loginMethod = a.getDeclaredMethod("login", String.class, String.class);

                    loginMethod.setAccessible(true);

                    AdminDao adminDao = new AdminDao();
                    Boolean result = (Boolean) loginMethod.invoke(adminDao,account,password);
                    if (result ==false) {
                        System.out.println("登录失败！");
                        frame3.dispose();
                    }

                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
        });
        frame3.pack();
        frame3.setVisible(true);
    }

}
